package cn.edu.zhku.service.impl;

import cn.edu.zhku.dto.GroupDto;
import cn.edu.zhku.mapper.form.FormMapper;
import cn.edu.zhku.pojo.Group;
import cn.edu.zhku.pojo.Task;
import cn.edu.zhku.mapper.TaskMapper;
import cn.edu.zhku.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private FormMapper formMapper;

    @Override
    @Transactional
    public void addOne(Task task) {
        this.taskMapper.insertOne(task);
    }

    @Override
    @Transactional
    public void distTaskBatch(Integer taskId, List<Integer> groupIds) {
        this.taskMapper.insertBatch2TaskGroup(taskId, groupIds);
    }

    @Override
    @Transactional
    public void modifyOne(Task task) {
        this.taskMapper.updateOne(task);
    }

    @Override
    @Transactional
    public void removeTaskBatch(List<Integer> taskIds) {
        this.taskMapper.deleteTaskBatch(taskIds);
        this.taskMapper.deleteGroupTaskBatch(taskIds);
        this.formMapper.deleteByTaskIdBatch(taskIds);
    }

    @Override
    @Transactional
    public void removeGroupBatch(Integer taskId, List<Integer> groupIds) {
        this.taskMapper.deleteGroupBatch(taskId, groupIds);
    }

    @Override
    public Task findById(Integer taskId) {
        return this.taskMapper.selectById(taskId);
    }

    @Override
    public boolean checkTaskExistByTaskIdBatch(List<Integer> taskIds) {
        return this.taskMapper.selectTaskNumByTaskIdBatch(taskIds) == taskIds.size();
    }

    @Override
    public boolean checkTaskGroupExistByTaskIdAndGroupIdBatch(Integer taskId, List<Integer> groupIds) {
        return this.taskMapper.selectTaskGroupNumByTaskIdAndGroupIdBatch(taskId, groupIds) == groupIds.size();
    }

    @Override
    public boolean checkTaskGroupNotExistByTaskIdAndGroupIdBatch(Integer taskId, List<Integer> groupIds) {
        return this.taskMapper.selectTaskGroupNumByTaskIdAndGroupIdBatch(taskId, groupIds) == 0;
    }

    @Override
    public List<Task> findTaskTea(Task task) {
        return this.taskMapper.selectTaskTea(task);
    }

    @Override
    public List<Group> findTaskGroupTea(GroupDto groupDto) {
        return this.taskMapper.selectTaskGroupTea(groupDto);
    }

    @Override
    public List<Group> findGroupNotInTaskTea(GroupDto groupDto) {
        return this.taskMapper.selectGroupNotInTaskTea(groupDto);
    }

    @Override
    public Integer getTaskGroupNum(Integer taskId) {
        return this.taskMapper.selectTaskGroupNum(taskId);
    }

}
